<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>请假管理</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="../../layuimini/lib/layui-v2.5.5/css/layui.css" media="all" />
  <link rel="stylesheet" href="../../layuimini/css/public.css" media="all" />
  <style>
    .laytable-cell-checkbox .layui-disabled.layui-form-checked i {
      background: #fff !important;
    }
  </style>
</head>

<body>
  <div class="layuimini-container">
    <div class="layuimini-main">
      <fieldset class="table-search-fieldset">
        <legend>搜索信息</legend>
        <div style="margin: 10px 10px 10px 10px">
          <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">职工:</label>
                <div class="layui-input-inline">
                  <select name="personalId" id="selectId" lay-search="">
                    <option value="">请选择请假人</option>
                  </select>
                </div>
              </div>

              <div class="layui-inline">
                <label class="layui-form-label">请假类型:</label>
                <div class="layui-input-block">
                  <select name="leaveType" id="type">
                    <option value="">请选择请假类型</option>
                    <option value="1">事假</option>
                    <option value="2">年假</option>
                    <option value="3">婚假</option>
                    <option value="4">产假</option>
                    <option value="5">陪产假</option>
                    <option value="6">丧假</option>
                    <option value="7">调休假</option>
                    <option value="8">其它</option>
                  </select>
                </div>
              </div>

              <div class="layui-inline">
                <label class="layui-form-label">状态:</label>
                <div class="layui-input-block">
                  <select name="leaveStatus" id="status">
                    <option value="">请选择状态</option>
                    <option value="1">已提交</option>
                    <option value="2">审批通过</option>
                    <option value="3">审批拒绝</option>
                  </select>
                </div>
              </div>

              <div class="layui-inline">
                <button type="submit" class="layui-btn" lay-submit lay-filter="data-search-btn">
                  <i class="layui-icon layui-icon-search"></i> 搜 索
                </button>

                <button type="button" class="layui-btn layui-btn-primary" id="data-reset-btn">
                  <i class="layui-icon"></i> 重 置
                </button>
              </div>
            </div>
          </form>
        </div>
      </fieldset>

      <script type="text/html" id="toolbarDemo">
          <div class="layui-btn-group">
            <button
              class="layui-btn layui-btn layui-btn-sm data-add-btn"
              lay-event="add"
            >
              添加
            </button>
            <button
              class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn"
              lay-event="delete"
            >
              删除
            </button>
            <button
              class="layui-btn layui-btn-sm layui-btn-warm"
              lay-event="refresh"
            >
              刷新
            </button>
            <button
              class="layui-btn layui-btn-sm layui-btn-normal"
              lay-event="export"
            >
              导出全部数据
            </button>
          </div>
        </script>

      <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

      <script type="text/html" id="currentTableBar">
          <div
            class="layui-btn layui-btn-normal layui-btn-xs data-count-edit"
            lay-event="edit"
          >
            编辑
          </div>
          <div
            class="layui-btn layui-btn-xs layui-btn-danger data-count-delete"
            lay-event="delete"
          >
            删除
          </div>
        </script>
    </div>
  </div>

  <!-- 新增编辑请假条 -->
  <script type="text/html" id="leaveEditDialog">
      <form
        id="userEditForm"
        lay-filter="userEditForm"
        class="layui-form layui-form-panes"
        style="padding-left:20px;margin-top: 20px;"
      >
        <input name="leaveId" type="hidden" />
        <div class="layui-row">
          <div class="layui-col-md12">
            <div class="layui-form-item">
              <label class="layui-form-label">请假人:</label>
              <div class="layui-input-inline">
                <select
                  lay-search=""
                  name="personalId"
                  id="leaver"
                  lay-verify="required"
                  lay-verType="tips"
                >
                  <option value="">请选择请假人</option>
                </select>
              </div>

              <label class="layui-form-label">请假类型:</label>
              <div class="layui-input-inline">
                <select name="leaveType" id="type" lay-verify="required" lay-verType="tips">
                  <option value="">请选择请假类型</option>
                  <option value="1">事假</option>
                  <option value="2">年假</option>
                  <option value="3">婚假</option>
                  <option value="4">产假</option>
                  <option value="5">陪产假</option>
                  <option value="6">丧假</option>
                  <option value="7">调休假</option>
                  <option value="8">其它</option>
                </select>
              </div>
            </div>

            <div class="layui-form-item">
              <label class="layui-form-label ">开始日期:</label>
              <div class="layui-input-inline">
                <input
                  placeholder="请假开始日期"
                  type="text"
                  name="leaveStartTime"
                  id="startTime"
                  autocomplete="off"
                  class="layui-input"
                  lay-verify="required"
                  lay-verType="tips"
                />
              </div>

              <label class="layui-form-label ">结束日期:</label>
              <div class="layui-input-inline">
                <input
                  placeholder="请假结束日期"
                  type="text"
                  name="leaveEndTime"
                  id="endTime"
                  autocomplete="off"
                  class="layui-input"
                  lay-verify="required"
                  lay-verType="tips"
                />
              </div>
            </div>

            <div class="layui-form-item">
              <label class="layui-form-label">请假原因:</label>
              <div class="layui-input-inline">
                <textarea
                  name="reason"
                  placeholder="请假原因"
                  class="layui-textarea"
                ></textarea>
              </div>
            </div>

            <div class="layui-form-item ">
              <label class="layui-form-label"></label>
              <button
                class="layui-btn layui-btn-normal"
                lay-filter="userEditSubmit"
                lay-submit
              >
                提交
              </button>
              <button
                class="layui-btn layui-btn-primary"
                type="button"
                ew-event="closeDialog"
                id="Cancel"
              >
                取消
              </button>
            </div>
          </div>
        </div>
      </form>
    </script>

  <script src="../../layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
  <script src="../../layuimini/js/lay-config.js" charset="utf-8"></script>

  <script>
    layui.use(
      ["layer", "form", "table", "laydate", "upload", "common", "excel"],
      function () {
        var $ = layui.jquery,
          form = layui.form,
          laydate = layui.laydate,
          table = layui.table,
          upload = layui.upload,
          common = layui.common,
          excel = layui.excel;

        tableIns = table.render({
          elem: "#currentTableId",
          url: common.api + "/leave/paged-list",
          where: { token: common.getToken },
          toolbar: "#toolbarDemo",
          title: "请假信息",
          limits: [1, 5, 10, 15, 20],
          limit: 10,
          page: true,
          skin: "row",
          even: true,
          cols: [
            [
              // {
              //   fixed: "left",
              //   // type: "checkbox",
              //   width: 50,
              //   title:"<input type='checkbox' name='siam_all' title='' lay-skin='primary' lay-filter='siam_all'>",
              //   templet: function (d) {
              //     if (d.leaveStatus == 1 || d.leaveStatus == 3) {
              //       return '<div><input type="checkbox" name="siam_one" title="" lay-skin="primary" data-id = "${d.id}"></div>';
              //     }
              //     return "";
              //   },
              // },
              {
                fixed: "left",
                width: 50,
                type: "checkbox"
              },


              {
                fixed: "left",
                field: "leaveId",
                title: "请假ID",
                width: 100,
                sort: true,
                align: "center",
              },
              {
                fixed: "left",
                field: "leaverName",
                width: 150,
                title: "职工姓名",
                align: "center",
              },
              {
                field: "leaveType",
                width: 150,
                title: "请假类型",
                align: "center",
                templet: function (d) {
                  if (d.leaveType == 1) {
                    return "事假";
                  } else if (d.leaveType == 2) {
                    return "年假";
                  } else if (d.leaveType == 3) {
                    return "婚假";
                  } else if (d.leaveType == 4) {
                    return "产假";
                  } else if (d.leaveType == 5) {
                    return "陪产假";
                  } else if (d.leaveType == 6) {
                    return "丧假";
                  } else if (d.leaveType == 7) {
                    return "调休假";
                  } else if (d.leaveType == 8) {
                    return "其它";
                  }
                  return "";
                },
              },
              {
                field: "leaveStartTime",
                width: 150,
                title: "请假开始日期",
                align: "center",
                sort: true,
              },
              {
                field: "leaveEndTime",
                width: 150,
                title: "请假结束日期",
                align: "center",
                sort: true,
              },
              {
                field: "reason",
                minWidth: 200,
                title: "请假原因",
                align: "center",
              },
              {
                field: "leaveStatus",
                width: 150,
                title: "状态",
                align: "center",
                sort: true,
                templet: function (d) {
                  if (d.leaveStatus == 1) {
                    return '<span class="layui-badge layui-bg-black">待审批</span>';
                  } else if (d.leaveStatus == 2) {
                    return '<span class="layui-badge layui-bg-green">审批通过</span>';
                  } else if (d.leaveStatus == 3) {
                    return '<span class="layui-badge layui-bg-red">审批拒绝</span>';
                  }
                  return "";
                },
              },
              {
                field: "approverName",
                width: 150,
                title: "审批人",
                align: "center",
              },
              {
                field: "approveRemark",
                width: 150,
                title: "审批意见",
                align: "center",
              },
              {
                field: "approveTime",
                minWidth: 200,
                title: "审批时间",
                align: "center",
              },
              {
                fixed: "right",
                title: "操作",
                width: 150,
                toolbar: "#currentTableBar",
                align: "center",
                // templet: function (d) {
                //   if (d.leaveStatus == 1 || d.leaveStatus == 3) {
                //     return '<div class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</div>'
                //       + '<div class="layui-btn layui-btn-xs layui-btn-danger data-count-delete"lay-event="delete">删除</div>'
                //   }
                //   return "";

                // }
              },
            ],
          ],

          parseData: function (res) {
            if (res.code != 200) {
              common.login();
            }
            return {
              code: res.code, //解析接口状态
              msg: res.msg, //解析提示文本
              count: res.data.total, //解析数据长度
              data: res.data.list, //解析数据列表
            };
          },
          response: {
            statusCode: 200, //重新规定成功的状态码为 200，table 组件默认为 0
          },
          request: {
            pageName: "page", //页码的参数名称，默认：page
            limitName: "size", //每页数据量的参数名，默认：limit
          },
          initSort: {
            field: "leaveId",
            type: "asc",
          },
        });

        //监听工具栏添加删除刷新
        table.on("toolbar(currentTableFilter)", function (obj) {
          if (obj.event === "add") {
            // 监听添加操作
            showEditModel();
          } else if (obj.event === "delete") {
            // 监听删除操作
            var data = table.checkStatus("currentTableId").data;

            // var ids = [];
            // $.each($("input[name=siam_one]:checked"), function (i, value) {
            //   console.log(111, value);
            //   ids[i] = $(this).attr("data-id");  // 如果需要获取其他的值 需要在模板中把值放到属性中 然后这里就可以拿到了
            // });
            // layer.alert(JSON.stringify(ids))
            if (data.length === 0) {
              layer.msg("请选择要删除的数据", { icon: 2 });
              return;
            }
            var ids = data.map(function (d) {
              return d.leaveId;
            });

            doDel(ids);
          } else if (obj.event === "refresh") {
            tableIns.reload({
              url: common.api + "/leave/paged-list",
              // where: {},
            });
          } else if (obj.event === "export") {
            common.ajax(common.api + "/leave/list", null, function (res) {
              var data = res.data;
              $.each(data, function (index, item) {
                if (item.leaveType == 1) {
                  item.leaveType = "事假";
                } else if (item.leaveType == 2) {
                  item.leaveType = "年假";
                } else if (item.leaveType == 3) {
                  item.leaveType = "婚假";
                } else if (item.leaveType == 4) {
                  item.leaveType = "产假";
                } else if (item.leaveType == 5) {
                  item.leaveType = "陪产假";
                } else if (item.leaveType == 6) {
                  item.leaveType = "丧假";
                } else if (item.leaveType == 7) {
                  item.leaveType = "调休假";
                } else if (item.leaveType == 8) {
                  item.leaveType = "其它";
                }

                if (item.leaveStatus == 1) {
                  item.leaveStatus = "已提交";
                } else if (item.leaveStatus == 2) {
                  item.leaveStatus = "审批通过";
                } else if (item.leaveStatus == 3) {
                  item.leaveStatus = "审批拒绝";
                }
              });
              data = common.compare(data);
              data = excel.filterExportData(data, [
                "leaveId",
                "leaverName",
                "leaveType",
                "leaveStartTime",
                "leaveEndTime",
                "reason",
                "leaveStatus",
                "approverName",
                "approveRemark",
                "approveTime",
              ]);
              data.unshift({
                leaveId: "请假ID",
                leaverName: "职工姓名",
                leaveType: "请假类型",
                leaveStartTime: "请假开始日期",
                leaveEndTime: "请假结束日期",
                reason: "请假原因",
                leaveStatus: "状态",
                approverName: "审批人",
                approveRemark: "审批意见",
                approveTime: "审批时间",
              });
              var timestart = Date.now();
              excel.exportExcel(
                {
                  sheet1: data,
                },
                "请假单列表.xlsx",
                "xlsx"
              );
              var timeend = Date.now();

              var spent = (timeend - timestart) / 1000;
              layer.msg("导出成功", { icon: 1 });
            });
          }
        });

        //监听编辑删除
        table.on("tool(currentTableFilter)", function (obj) {
          if (obj.event === "edit") {
            showEditModel(obj.data);
          } else if (obj.event === "delete") {
            doDel([obj.data.leaveId]);
          }
        });
        // 全选
        // form.on("checkbox(siam_all)", function () {
        //   var status = $(this).prop("checked");
        //   $.each($("input[name=siam_one]"), function (i, value) {
        //     $(this).prop("checked", status);
        //   });
        //   form.render();
        // });

        function showEditModel(formData) {
          var idx = layer.open({
            type: 1,
            area: ["700px", "350px"],
            title: formData ? "编辑" : "添加",

            anim: 1,
            shade: 0.6,
            content: $("#leaveEditDialog").html(),
            success: function (layero, dIndex) {
              // 回显表单数据
              form.val("userEditForm", formData);

              $.each(selectData, function (index, item) {
                var option = new Option(item.name, item.personalId);
                $("#leaver").append(option);
              });
              var index = $("#leaver").val();
              if (formData) {
                index = formData.personalId;
                $("#leaver").val(formData.personalId);
                $("#type").val(formData.leaveType);
              }

              form.render("select");
              //加载日期
              laydate.render({
                elem: "#startTime",
                value: formData ? formData.leaveStartTime : "",
              });
              laydate.render({
                elem: "#endTime",
                value: formData ? formData.leaveEndTime : "",
              });

              // 表单提交事件
              form.on("submit(userEditSubmit)", function (data) {
                console.log(222, formData);
                common.ajax(
                  formData
                    ? common.api + "/leave/update"
                    : common.api + "/leave/create",
                  JSON.stringify(data.field),
                  function (res) {
                    if (res.code === 200) {
                      layer.close(dIndex);
                      layer.msg(res.msg, { icon: 1 });
                      setTimeout(() => {
                        table.reload("currentTableId");
                      }, 1000);
                    } else {
                      layer.msg(res.msg, { icon: 2 });
                    }
                  },
                  formData ? "put" : "post",
                  function (data) {
                    layer.msg(data.msg, { icon: 2 });
                  },
                  false,
                  true
                );
                return false;
              });

              $("#Cancel").click(function () {
                layer.close(idx);
              });
            },
          });
        }

        function doDel(ids) {
          layer.confirm(
            "确定要删除选中数据吗？",
            {
              skin: "layui-layer-admin",
              shade: 0.1,
            },
            function (i) {
              common.ajax(
                common.api + "/leave/delete",
                JSON.stringify(ids),
                function (res) {
                  if (res.code === 200) {
                    layer.msg(res.msg, { icon: 1 });
                    tableIns.reload();
                  } else {
                    layer.msg(res.msg, { icon: 2 });
                  }
                },
                "DELETE",
                function (data) {
                  layer.msg(data.msg, { icon: 2, time: 1000 });
                  return;
                }
              );
              layer.close(i);
            }
          );
        }

        // 监听搜索操作
        form.on("submit(data-search-btn)", function (data) {
          var param = data.field;
          // return false
          // 执行搜索重载
          tableIns.reload({
            url: common.api + "/leave/paged-list",
            method: "get",
            where: param,
          });
          return false;
        });

        var selectData;
        layer.ready(function () {
          // select
          common.ajax(common.api + "/personal/all", null, function (data) {
            selectData = data.data;
            $.each(selectData, function (index, item) {
              var option = new Option(item.name, item.personalId);
              $("#selectId").append(option);
              console.log(JSON.stringify(222, item));
            });
            form.render("select");
          });
          // 监听重置操作
          $("#data-reset-btn").click(function () {
            $("#selectId").val(0);
            $("#type").val(0);
            $("#status").val(0);
            form.render("select");
            tableIns.reload({
              url: common.api + "/leave/paged-list",
              method: "get",
              where: { personalId: "", leaveType: "", leaveStatus: "" },
            });
          });
        });
      }
    );
  </script>
</body>

</html>